package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.system.domain.StringId;
import com.ruoyi.system.domain.comment.FileComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ModuleFileInfo;
import com.ruoyi.system.service.IModuleFileInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import org.springframework.http.ResponseEntity;

/**
 * 文件信息Controller
 *
 * @author ruoyi
 * @date 2023-07-12
 */
@RestController
@RequestMapping("/system/fileinfo")
public class ModuleFileInfoController extends BaseController {
    @Autowired
    private IModuleFileInfoService moduleFileInfoService;

    /**
     * 查询文件信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModuleFileInfo moduleFileInfo) {
        startPage();
        PageHelper.orderBy("efilenum");
        List<ModuleFileInfo> list = moduleFileInfoService.selectModuleFileInfoList(moduleFileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:export')")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModuleFileInfo moduleFileInfo) {
        List<ModuleFileInfo> list = moduleFileInfoService.selectModuleFileInfoList(moduleFileInfo);
        ExcelUtil<ModuleFileInfo> util = new ExcelUtil<ModuleFileInfo>(ModuleFileInfo.class);
        util.exportExcel(response, list, "文件信息数据");
    }

    /**
     * 查询文件注释信息列表
     *
     * @return 文件信息集合
     */
    @PostMapping("/getFileCommentInfo")
    public List<FileComment> getFileCommentInfot() {
        return moduleFileInfoService.selectModuleFileComInfoList();
    }

    /**
     * 获取文件信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(moduleFileInfoService.selectModuleFileInfoById(id));
    }

    /**
     * 查询文件信息通过id
     */
    @PostMapping("/getFileInfoById")
    public AjaxResult getFileInfoById(@RequestBody StringId stringId) {
        if (stringId.getIds() != null) {
            String[] stringList = stringId.getIds();
            String id = stringList[0];
            return success(moduleFileInfoService.selectModuleFileInfoById(id));
        } else {
            String id = stringId.getId();
            return success(moduleFileInfoService.selectModuleFileInfoById(id));
        }
    }

    /**
     * 获取二进制文件
     */
    @PostMapping("/getPdf")
    public ResponseEntity<byte[]> getPdf(@RequestBody ModuleFileInfo FileInfo) {
        return moduleFileInfoService.getpdf(FileInfo.getId());
    }

    /**
     * 获取文件类型
     */
    @PostMapping("/getType")
    public String getType(@RequestBody ModuleFileInfo FileInfo) {
        return moduleFileInfoService.getType(FileInfo.getId());
    }

    /**
     * 新增文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:add')")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModuleFileInfo moduleFileInfo) {
        return toAjax(moduleFileInfoService.insertModuleFileInfo(moduleFileInfo));
    }

    /**
     * 修改文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:edit')")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModuleFileInfo moduleFileInfo) {
        return toAjax(moduleFileInfoService.updateModuleFileInfo(moduleFileInfo));
    }

    /**
     * 删除文件信息通过stringId
     */
    @PostMapping("/removeFileInfoById")
    public AjaxResult removeFileInfoById(@RequestBody StringId stringId) {
        if (stringId.getIds() != null) {
            String[] ids = stringId.getIds();
            return toAjax(moduleFileInfoService.deleteModuleFileInfoByIds(ids));
        } else {
            String id = stringId.getId();
            return toAjax(moduleFileInfoService.deleteModuleFileInfoById(id));
        }
    }

    /**
     * 删除文件信息
     */
    // @PreAuthorize("@ss.hasPermi('system:fileinfo:remove')")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(moduleFileInfoService.deleteModuleFileInfoByIds(ids));
    }

    @GetMapping("downloadMore/{ids}")
    public ResponseEntity<byte[]> downloadMore(@PathVariable String[] ids) {
        return moduleFileInfoService.downloadMore(ids);
    }

}
